// pages/limitBuy/limitBuy.js
const app = getApp();
import httpsUtils from '../../utils/https.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["服装", "皮靴", "箱包"],
    activeIndex: 0,
    timeTab: ["今日抢批","明日预告"],
    nowIndex: 0,
    limitList: [],//服装今日热卖抢批列表
    limitTotal: 1,
    limitLists: [],//服装今日抢批列表
    limitTotals: 1,
    clothWillList: [],//服装明日预告列表
    clothWillTotal: 1,
    shoesNewHotList: [],//皮靴今日热卖抢批列表
    shoesNewHotTotal: 1,
    shoesNewList: [],//皮靴今日抢批列表
    shoesNewTotal: 1,
    shoesWillList: [],//皮靴明日预告列表
    shoesWillTotal: 1,
    boxNewHotList: [],//箱包今日热卖抢批列表
    boxNewHotTotal: 1,
    boxNewList: [],//箱包今日抢批列表
    boxNewTotal: 1,
    boxWillList: [],//箱包明日预告列表
    boxWillTotal: 1,
    limitDeta: {},//限时抢购时间

    isBuy:0,//默认0表示不能抢购，1表示能抢购
    hourTime: '00',
    minuteTime: '00',
    secondTime: '00',
    tomHourTime: '00',
    tomMinuteTime: '00',
    tomSecondTime: '00',
  },
  /**
   * 设置提醒
   */
  setPush:function(e){
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/pushRushGoodsMsg?goodsId=' + e.currentTarget.dataset.goodsId,
      data: {},
    }, function (json) {
      if (json.success) {
       wx.showToast({
         title: '设置提醒成功',
       })
      }else{
        wx.showToast({
          title: '已设置提醒',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    /*服装今日抢批热卖*/ 
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/selectRushGoodsList1?timeStatus=1&type=1&size=3',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          limitList: json.data.list,
          limitTotal: json.data.total
        })
      }
    })
    /*服装今日抢批*/
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/selectRushGoodsList1?timeStatus=1&type=1',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        var limit_list = json.data.list.slice(3);
        that.setData({
          limitLists: limit_list,
          limitTotals: json.data.total
        })
      }
    })
    wx.hideLoading();
    /*服装明日预告*/
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/selectRushGoodsList1?timeStatus=2&type=1',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        var limit_list = json.data.list;
        that.setData({
          clothWillList: limit_list,
          clothWillTotal: json.data.total
        })
      }
    })
    /*皮靴今日热卖抢批*/ 
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/selectRushGoodsList1?timeStatus=1&type=2&size=3',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          shoesNewHotList: json.data.list,
          shoesNewHotTotal: json.data.total
        })
      }
    })
    /*皮靴今日抢批*/
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/selectRushGoodsList1?timeStatus=1&type=2',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        var limit_list = json.data.list.slice(3);
        that.setData({
          shoesNewList: limit_list,
          shoesNewTotal: json.data.total
        })
      }
    })
    /*皮靴明日预告*/
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/selectRushGoodsList1?timeStatus=2&type=2',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        var limit_list = json.data.list;
        that.setData({
          shoesWillList: limit_list,
          shoesWillTotal: json.data.total
        })
      }
    })
    /*箱包今日热卖抢批*/
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/selectRushGoodsList1?timeStatus=1&type=3&size=3',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          boxNewHotList: json.data.list,
          boxNewHotTotal: json.data.total
        })
      }
    })
    /*箱包今日抢批*/
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/selectRushGoodsList1?timeStatus=1&type=3',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        var limit_list = json.data.list.slice(3);
        that.setData({
          boxNewList: limit_list,
          boxNewTotal: json.data.total
        })
      }
    })
    /*箱包明日预告*/
    httpsUtils.getData({
      _url: app.globalData._url + '/rushGoods/selectRushGoodsList1?timeStatus=2&type=3',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        var limit_list = json.data.list;
        that.setData({
          boxWillList: limit_list,
          boxWillTotal: json.data.total
        })
      }
    })
    /*限时抢购距离结束时间*/
    httpsUtils.getData({
      _url: app.globalData._url + '/rushTime/getRushTime',
      data: '',
    }, function cutdown(json) {
        var new_date = new Date();
        var years = new_date.getFullYear();    // 获取完整的年份(4位,1970-????)
        var month = ("0" + (new_date.getMonth() + 1)).slice(-2);       // 获取当前月份(0-11,0代表1月)
        var days = new_date.getDate();        // 获取当前日(1-31)
        var new_date_times = new_date.getTime();        // 获取当前时间(从1970.1.1开始的毫秒数)
      var currentTimes = years + "/" + month + "/" + days;
        var end_times = currentTimes + ' ' +json.data.endTime;
        var start_times = currentTimes + ' ' + json.data.startTime;
        var end_date_times = new Date(end_times).getTime();//获取毫秒值
        var start_date_times = new Date(start_times).getTime();
        var tom_start_date_times = start_date_times + 86400000; //明天开始时间
        if (json.data && json.success) {
          if (start_date_times <= new_date_times && end_date_times >= new_date_times){
              var diff_time = Math.floor((end_date_times - new_date_times) / 1000);
              var hour_time = ("0" + Math.floor(diff_time / 3600 % 24)).slice(-2);
              var minute_time = ("0" + Math.floor(diff_time / 60 % 60)).slice(-2);
              var second_time = ("0" + Math.floor(diff_time % 60)).slice(-2);
              that.setData({
                isBuy: 1,
                hourTime: hour_time,
                minuteTime: minute_time,
                secondTime: second_time,
              })
            } else if (start_date_times > new_date_times) {
              var diff_time = Math.floor((start_date_times - new_date_times) / 1000);
              var hour_time = ("0" + Math.floor(diff_time / 3600 % 24)).slice(-2);
              var minute_time = ("0" + Math.floor(diff_time / 60 % 60)).slice(-2);
              var second_time = ("0" + Math.floor(diff_time % 60)).slice(-2);
              that.setData({
                isBuy: 0,
                hourTime: hour_time,
                minuteTime: minute_time,
                secondTime: second_time,
              })
            }else{
              that.setData({
                isBuy:0,
                hourTime: "00",
                minuteTime: "00",
                secondTime: "00",
              })
            }

          
            var tom_diff_time = Math.floor((tom_start_date_times - new_date_times) / 1000);
            var tom_hour_time = ("0" + Math.floor(tom_diff_time / 3600)).slice(-2);
            var tom_minute_time = ("0" + Math.floor(tom_diff_time / 60 % 60 )).slice(-2);
            var tom_second_time = ("0" + Math.floor(tom_diff_time % 60)).slice(-2);
            that.setData({
              tomHourTime: tom_hour_time,
              tomMinuteTime: tom_minute_time,
              tomSecondTime: tom_second_time,
              limitDeta: json.data,
            })

            setTimeout(function () {
              cutdown(json)
            }, 1000)
        }
    })
  },
  /**
   * 跳转进货车
   */
  toCart: function (options){
    var that = this;
    if (that.data.isBuy==0){
      wx.showToast({
        title: '暂未开始抢批',
      })
      return;
    }
    wx.navigateTo({
      url: '../addCart/addCart?id=' + options.currentTarget.dataset.goodsId + '&goodsType=3&cheapPrice=' + options.currentTarget.dataset.goodsPrice,
    })
  },
  /**
 * 生命周期函数--监听页面初次渲染完成
 */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */

  onShow: function () {
    var that = this;
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  TimeTabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      nowIndex: e.currentTarget.id
    });
  },
  /*取消订单弹框*/
  cancelOrder: function (e) {
    wx.showModal({
      title: '提示',
      content: '你确定要取消订单吗？',
      cancelColor: '#0F7FFE',
      confirmColor: '#0F7FFE',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  confirmOrder: function (e) {
    wx.navigateTo({
      url: '../pay/pay',
      success: function (res) { },
      fail: function (res) {
        wx.showToast({
          title: '支付失败',
        })
      },
      complete: function (res) { },
    })
  },
  getData: function (params, cb) {
    wx.request({
      url: params._url,
      data: params.data,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          typeof cb === 'function' && cb(res.data)
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  }
})